#ifndef CONFIG_MANAGER_H
#define CONFIG_MANAGER_H

#include "common.h"
#include "config_manager.h"
#include "logger.h"
#include <stdlib.h>
#include <string.h>

int config_manager_init(config_manager_t *config, db_manager_t *db_mgr, bool auto_reload) {
    if (!config || !db_mgr) {
        return ERROR_INVALID_PARAM;
    }

    memset(config, 0, sizeof(config_manager_t));
    config->db_mgr = db_mgr;
    config->auto_reload = auto_reload;
    config->last_reload = time(NULL);

    // 从数据库加载初始配置
    return config_manager_reload(config);
}

json_t *config_manager_get(config_manager_t *config, const char *path) {
    if (!config || !path) {
        return NULL;
    }

    // 从数据库获取配置
    return db_config_get_json(config->db_mgr, path);
}

int config_manager_set(config_manager_t *config, const char *path, json_t *value) {
    if (!config || !path || !value) {
        return ERROR_INVALID_PARAM;
    }

    char *value_str = json_dumps(value, JSON_COMPACT);
    if (!value_str) {
        return ERROR_JSON_PARSE;
    }

    int rc = db_config_set(config->db_mgr, path, value_str);
    free(value_str);

    return rc;
}

int config_manager_reload(config_manager_t *config) {
    if (!config) {
        return ERROR_INVALID_PARAM;
    }

    // 对于数据库配置，重新加载可能不需要特殊处理
    // 因为每次读取都是直接从数据库读取的
    config->last_reload = time(NULL);

    return SUCCESS;
}

int config_manager_save(config_manager_t *config) {
    // 数据库配置是实时保存的，所以这个函数可能不需要做太多事情
    return SUCCESS;
}

api_config_t *config_get_api_config(config_manager_t *config, const char *api_name) {
    if (!config || !api_name) {
        return NULL;
    }

    char path[256];
    snprintf(path, sizeof(path), "apis.%s", api_name);

    json_t *api_json = config_manager_get(config, path);
    if (!api_json || !json_is_object(api_json)) {
        return NULL;
    }

    api_config_t *api_config = malloc(sizeof(api_config_t));
    if (!api_config) {
        return NULL;
    }

    memset(api_config, 0, sizeof(api_config_t));

    // 解析API配置
    json_t *tmp;

    api_config->name = strdup(api_name);

    tmp = json_object_get(api_json, "url");
    if (tmp && json_is_string(tmp)) {
        api_config->url = strdup(json_string_value(tmp));
    }

    tmp = json_object_get(api_json, "method");
    if (tmp && json_is_string(tmp)) {
        api_config->method = strdup(json_string_value(tmp));
    } else {
        api_config->method = strdup("GET");
    }

    api_config->headers = json_object_get(api_json, "headers");
    if (api_config->headers) {
        json_incref(api_config->headers);
    }

    api_config->params = json_object_get(api_json, "params");
    if (api_config->params) {
        json_incref(api_config->params);
    }

    api_config->body = json_object_get(api_json, "body");
    if (api_config->body) {
        json_incref(api_config->body);
    }

    tmp = json_object_get(api_json, "timeout_ms");
    if (tmp && json_is_integer(tmp)) {
        api_config->timeout_ms = json_integer_value(tmp);
    } else {
        api_config->timeout_ms = 5000;
    }

    tmp = json_object_get(api_json, "interval_ms");
    if (tmp && json_is_integer(tmp)) {
        api_config->interval_ms = json_integer_value(tmp);
    } else {
        api_config->interval_ms = 60000;
    }

    tmp = json_object_get(api_json, "data_mapping");
    if (tmp && json_is_string(tmp)) {
        api_config->data_mapping = strdup(json_string_value(tmp));
    }

    tmp = json_object_get(api_json, "enabled");
    if (tmp && json_is_boolean(tmp)) {
        api_config->enabled = json_boolean_value(tmp);
    } else {
        api_config->enabled = true;
    }

    json_decref(api_json);
    return api_config;
}

// 其他配置管理器函数实现...
typedef struct {
    char *config_file;
    json_t *root;
    time_t last_modified;
    // nng_mtx *mutex;
} config_manager_t;

typedef struct {
    char *api_url;
    char *api_method;
    json_t *api_headers;
    json_t *api_params;
    int timeout_ms;
} api_config_t;

typedef struct {
    char *key_pattern;
    int check_interval;
    json_t *thresholds;
    char *event_name;
} monitor_config_t;

int config_manager_init(config_manager_t *config, const char *config_file);
json_t *config_manager_get(config_manager_t *config, const char *path);
int config_manager_reload(config_manager_t *config);
api_config_t *config_get_api_config(config_manager_t *config, const char *api_name);
monitor_config_t *config_get_monitor_config(config_manager_t *config, const char *monitor_name);
void config_manager_destroy(config_manager_t *config);

#endif
